{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-05-21T15:03:14.705363Z",
     "start_time": "2025-05-21T15:03:14.690890Z"
    }
   },
   "source": [
    "{\n",
    " \"cells\": [\n",
    "  {\n",
    "   \"metadata\": {\n",
    "    \"ExecuteTime\": {\n",
    "     \"end_time\": \"2025-05-08T07:17:00.417084Z\",\n",
    "     \"start_time\": \"2025-05-08T07:16:57.653970Z\"\n",
    "    }\n",
    "   },\n",
    "   \"cell_type\": \"code\",\n",
    "   \"source\": [\n",
    "    \"#随机初始化权重\\n\",\n",
    "    \"import numpy as np\\n\",\n",
    "    \"\\n\",\n",
    "    \"# 定义激活函数和其导数\\n\",\n",
    "    \"def sigmoid(x):\\n\",\n",
    "    \"    return 1 / (1 + np.exp(-x))\\n\",\n",
    "    \"\\n\",\n",
    "    \"def sigmoid_derivative(x):\\n\",\n",
    "    \"    return x * (1 - x)\\n\",\n",
    "    \"\\n\",\n",
    "    \"# 定义训练数据（异或问题）\\n\",\n",
    "    \"inputs = np.array([[0,0],[0,1],[1,0],[1,1]])\\n\",\n",
    "    \"expected_output = np.array([[0],[1],[1],[0]])\\n\",\n",
    "    \"\\n\",\n",
    "    \"# 网络参数\\n\",\n",
    "    \"input_size = 2\\n\",\n",
    "    \"hidden_size = 3\\n\",\n",
    "    \"output_size = 1\\n\",\n",
    "    \"learning_rate = 0.1\\n\",\n",
    "    \"epochs = 10000\\n\",\n",
    "    \"num_init = 10  # 尝试不同的随机初始化次数\\n\",\n",
    "    \"\\n\",\n",
    "    \"# 存储最佳模型\\n\",\n",
    "    \"best_loss = float('inf')\\n\",\n",
    "    \"best_weights = None\\n\",\n",
    "    \"best_biases = None\\n\",\n",
    "    \"\\n\",\n",
    "    \"for i in range(num_init):\\n\",\n",
    "    \"    # 随机初始化权重和偏置\\n\",\n",
    "    \"    weights_input_hidden = np.random.rand(input_size, hidden_size)\\n\",\n",
    "    \"    biases_hidden = np.random.rand(hidden_size)\\n\",\n",
    "    \"    weights_hidden_output = np.random.rand(hidden_size, output_size)\\n\",\n",
    "    \"    biases_output = np.random.rand(output_size)\\n\",\n",
    "    \"\\n\",\n",
    "    \"    for epoch in range(epochs):\\n\",\n",
    "    \"        # 前向传播\\n\",\n",
    "    \"        hidden_layer_activation = sigmoid(np.dot(inputs, weights_input_hidden) + biases_hidden)\\n\",\n",
    "    \"        predictions = sigmoid(np.dot(hidden_layer_activation, weights_hidden_output) + biases_output)\\n\",\n",
    "    \"\\n\",\n",
    "    \"        # 计算误差\\n\",\n",
    "    \"        error = expected_output - predictions\\n\",\n",
    "    \"        loss = np.mean(np.square(error))\\n\",\n",
    "    \"\\n\",\n",
    "    \"        # 反向传播\\n\",\n",
    "    \"        d_output = error * sigmoid_derivative(predictions)\\n\",\n",
    "    \"        error_hidden_layer = d_output.dot(weights_hidden_output.T)\\n\",\n",
    "    \"        d_hidden_layer = error_hidden_layer * sigmoid_derivative(hidden_layer_activation)\\n\",\n",
    "    \"\\n\",\n",
    "    \"        # 更新权重和偏置\\n\",\n",
    "    \"        weights_hidden_output += hidden_layer_activation.T.dot(d_output) * learning_rate\\n\",\n",
    "    \"        biases_output += np.sum(d_output, axis=0) * learning_rate\\n\",\n",
    "    \"        weights_input_hidden += inputs.T.dot(d_hidden_layer) * learning_rate\\n\",\n",
    "    \"        biases_hidden += np.sum(d_hidden_layer, axis=0) * learning_rate\\n\",\n",
    "    \"\\n\",\n",
    "    \"    # 保存最小损失对应的权重和偏置\\n\",\n",
    "    \"    if loss < best_loss:\\n\",\n",
    "    \"        best_loss = loss\\n\",\n",
    "    \"        best_weights = (weights_input_hidden, weights_hidden_output)\\n\",\n",
    "    \"        best_biases = (biases_hidden, biases_output)\\n\",\n",
    "    \"\\n\",\n",
    "    \"# 使用最佳模型进行预测\\n\",\n",
    "    \"weights_input_hidden, weights_hidden_output = best_weights\\n\",\n",
    "    \"biases_hidden, biases_output = best_biases\\n\",\n",
    "    \"hidden_layer_activation = sigmoid(np.dot(inputs, weights_input_hidden) + biases_hidden)\\n\",\n",
    "    \"predictions = sigmoid(np.dot(hidden_layer_activation, weights_hidden_output) + biases_output)\\n\",\n",
    "    \"\\n\",\n",
    "    \"print(\\\"Final predictions:\\\")\\n\",\n",
    "    \"print(predictions)\\n\",\n",
    "    \"\\n\"\n",
    "   ],\n",
    "   \"id\": \"f9edc28a6869fafe\",\n",
    "   \"outputs\": [\n",
    "    {\n",
    "     \"name\": \"stdout\",\n",
    "     \"output_type\": \"stream\",\n",
    "     \"text\": [\n",
    "      \"Final predictions:\\n\",\n",
    "      \"[[0.05461413]\\n\",\n",
    "      \" [0.94973818]\\n\",\n",
    "      \" [0.94972971]\\n\",\n",
    "      \" [0.05362768]]\\n\"\n",
    "     ]\n",
    "    }\n",
    "   ],\n",
    "   \"execution_count\": 7\n",
    "  },\n",
    "  {\n",
    "   \"metadata\": {\n",
    "    \"ExecuteTime\": {\n",
    "     \"end_time\": \"2025-05-08T07:17:00.583126Z\",\n",
    "     \"start_time\": \"2025-05-08T07:17:00.430443Z\"\n",
    "    }\n",
    "   },\n",
    "   \"cell_type\": \"code\",\n",
    "   \"source\": [\n",
    "    \"#动量\\n\",\n",
    "    \"# 动量参数\\n\",\n",
    "    \"momentum = 0.9\\n\",\n",
    "    \"velocity_w_input_hidden = np.zeros_like(weights_input_hidden)\\n\",\n",
    "    \"velocity_b_hidden = np.zeros_like(biases_hidden)\\n\",\n",
    "    \"velocity_w_hidden_output = np.zeros_like(weights_hidden_output)\\n\",\n",
    "    \"velocity_b_output = np.zeros_like(biases_output)\\n\",\n",
    "    \"\\n\",\n",
    "    \"for epoch in range(epochs):\\n\",\n",
    "    \"    # 前向传播、计算误差、反向传播的代码（与上面相同）\\n\",\n",
    "    \"\\n\",\n",
    "    \"    # 更新权重和偏置时加入动量\\n\",\n",
    "    \"    velocity_w_hidden_output = momentum * velocity_w_hidden_output + learning_rate * hidden_layer_activation.T.dot(d_output)\\n\",\n",
    "    \"    velocity_b_output = momentum * velocity_b_output + learning_rate * np.sum(d_output, axis=0)\\n\",\n",
    "    \"    velocity_w_input_hidden = momentum * velocity_w_input_hidden + learning_rate * inputs.T.dot(d_hidden_layer)\\n\",\n",
    "    \"    velocity_b_hidden = momentum * velocity_b_hidden + learning_rate * np.sum(d_hidden_layer, axis=0)\\n\",\n",
    "    \"\\n\",\n",
    "    \"    weights_hidden_output += velocity_w_hidden_output\\n\",\n",
    "    \"    biases_output += velocity_b_output\\n\",\n",
    "    \"    weights_input_hidden += velocity_w_input_hidden\\n\",\n",
    "    \"    biases_hidden += velocity_b_hidden\\n\"\n",
    "   ],\n",
    "   \"id\": \"77accfb38aa7a37c\",\n",
    "   \"outputs\": [],\n",
    "   \"execution_count\": 8\n",
    "  },\n",
    "  {\n",
    "   \"metadata\": {\n",
    "    \"ExecuteTime\": {\n",
    "     \"end_time\": \"2025-05-08T07:17:00.594164Z\",\n",
    "     \"start_time\": \"2025-05-08T07:17:00.588850Z\"\n",
    "    }\n",
    "   },\n",
    "   \"cell_type\": \"code\",\n",
    "   \"source\": [\n",
    "    \"#使用优化器\\n\",\n",
    "    \"import tensorflow as tf\\n\",\n",
    "    \"\\n\",\n",
    "    \"# 假设inputs是输入数据，weights是权重\\n\",\n",
    "    \"inputs = tf.constant([[0.0, 0.0], [0.0, 1.0], [1.0, 0.0], [1.0, 1.0]], dtype=tf.float32)\\n\",\n",
    "    \"weights = tf.random.normal([2, 3], dtype=tf.float32)  # 假设权重是2x3的矩阵\\n\",\n",
    "    \"\\n\",\n",
    "    \"# 确保inputs和weights的数据类型一致\\n\",\n",
    "    \"inputs = tf.cast(inputs, tf.float32)\\n\",\n",
    "    \"weights = tf.cast(weights, tf.float32)\\n\",\n",
    "    \"\\n\",\n",
    "    \"# 执行矩阵乘法\\n\",\n",
    "    \"hiddenlayeractivation = tf.matmul(inputs, weights)\\n\",\n",
    "    \"\\n\",\n",
    "    \"print(hiddenlayeractivation)\\n\",\n",
    "    \"\\n\"\n",
    "   ],\n",
    "   \"id\": \"aa0b0930cccd3bb3\",\n",
    "   \"outputs\": [\n",
    "    {\n",
    "     \"name\": \"stdout\",\n",
    "     \"output_type\": \"stream\",\n",
    "     \"text\": [\n",
    "      \"tf.Tensor(\\n\",\n",
    "      \"[[ 0.         0.         0.       ]\\n\",\n",
    "      \" [ 1.7745123  1.0953304 -1.9704698]\\n\",\n",
    "      \" [-3.2716508 -1.5692956 -1.4103999]\\n\",\n",
    "      \" [-1.4971385 -0.4739653 -3.3808699]], shape=(4, 3), dtype=float32)\\n\"\n",
    "     ]\n",
    "    }\n",
    "   ],\n",
    "   \"execution_count\": 9\n",
    "  }\n",
    " ],\n",
    " \"metadata\": {\n",
    "  \"kernelspec\": {\n",
    "   \"display_name\": \"Python 3\",\n",
    "   \"language\": \"python\",\n",
    "   \"name\": \"python3\"\n",
    "  },\n",
    "  \"language_info\": {\n",
    "   \"codemirror_mode\": {\n",
    "    \"name\": \"ipython\",\n",
    "    \"version\": 2\n",
    "   },\n",
    "   \"file_extension\": \".py\",\n",
    "   \"mimetype\": \"text/x-python\",\n",
    "   \"name\": \"python\",\n",
    "   \"nbconvert_exporter\": \"python\",\n",
    "   \"pygments_lexer\": \"ipython2\",\n",
    "   \"version\": \"2.7.6\"\n",
    "  }\n",
    " },\n",
    " \"nbformat\": 4,\n",
    " \"nbformat_minor\": 5\n",
    "}\n"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'cells': [{'metadata': {'ExecuteTime': {'end_time': '2025-05-08T07:17:00.417084Z',\n",
       "     'start_time': '2025-05-08T07:16:57.653970Z'}},\n",
       "   'cell_type': 'code',\n",
       "   'source': ['#随机初始化权重\\n',\n",
       "    'import numpy as np\\n',\n",
       "    '\\n',\n",
       "    '# 定义激活函数和其导数\\n',\n",
       "    'def sigmoid(x):\\n',\n",
       "    '    return 1 / (1 + np.exp(-x))\\n',\n",
       "    '\\n',\n",
       "    'def sigmoid_derivative(x):\\n',\n",
       "    '    return x * (1 - x)\\n',\n",
       "    '\\n',\n",
       "    '# 定义训练数据（异或问题）\\n',\n",
       "    'inputs = np.array([[0,0],[0,1],[1,0],[1,1]])\\n',\n",
       "    'expected_output = np.array([[0],[1],[1],[0]])\\n',\n",
       "    '\\n',\n",
       "    '# 网络参数\\n',\n",
       "    'input_size = 2\\n',\n",
       "    'hidden_size = 3\\n',\n",
       "    'output_size = 1\\n',\n",
       "    'learning_rate = 0.1\\n',\n",
       "    'epochs = 10000\\n',\n",
       "    'num_init = 10  # 尝试不同的随机初始化次数\\n',\n",
       "    '\\n',\n",
       "    '# 存储最佳模型\\n',\n",
       "    \"best_loss = float('inf')\\n\",\n",
       "    'best_weights = None\\n',\n",
       "    'best_biases = None\\n',\n",
       "    '\\n',\n",
       "    'for i in range(num_init):\\n',\n",
       "    '    # 随机初始化权重和偏置\\n',\n",
       "    '    weights_input_hidden = np.random.rand(input_size, hidden_size)\\n',\n",
       "    '    biases_hidden = np.random.rand(hidden_size)\\n',\n",
       "    '    weights_hidden_output = np.random.rand(hidden_size, output_size)\\n',\n",
       "    '    biases_output = np.random.rand(output_size)\\n',\n",
       "    '\\n',\n",
       "    '    for epoch in range(epochs):\\n',\n",
       "    '        # 前向传播\\n',\n",
       "    '        hidden_layer_activation = sigmoid(np.dot(inputs, weights_input_hidden) + biases_hidden)\\n',\n",
       "    '        predictions = sigmoid(np.dot(hidden_layer_activation, weights_hidden_output) + biases_output)\\n',\n",
       "    '\\n',\n",
       "    '        # 计算误差\\n',\n",
       "    '        error = expected_output - predictions\\n',\n",
       "    '        loss = np.mean(np.square(error))\\n',\n",
       "    '\\n',\n",
       "    '        # 反向传播\\n',\n",
       "    '        d_output = error * sigmoid_derivative(predictions)\\n',\n",
       "    '        error_hidden_layer = d_output.dot(weights_hidden_output.T)\\n',\n",
       "    '        d_hidden_layer = error_hidden_layer * sigmoid_derivative(hidden_layer_activation)\\n',\n",
       "    '\\n',\n",
       "    '        # 更新权重和偏置\\n',\n",
       "    '        weights_hidden_output += hidden_layer_activation.T.dot(d_output) * learning_rate\\n',\n",
       "    '        biases_output += np.sum(d_output, axis=0) * learning_rate\\n',\n",
       "    '        weights_input_hidden += inputs.T.dot(d_hidden_layer) * learning_rate\\n',\n",
       "    '        biases_hidden += np.sum(d_hidden_layer, axis=0) * learning_rate\\n',\n",
       "    '\\n',\n",
       "    '    # 保存最小损失对应的权重和偏置\\n',\n",
       "    '    if loss < best_loss:\\n',\n",
       "    '        best_loss = loss\\n',\n",
       "    '        best_weights = (weights_input_hidden, weights_hidden_output)\\n',\n",
       "    '        best_biases = (biases_hidden, biases_output)\\n',\n",
       "    '\\n',\n",
       "    '# 使用最佳模型进行预测\\n',\n",
       "    'weights_input_hidden, weights_hidden_output = best_weights\\n',\n",
       "    'biases_hidden, biases_output = best_biases\\n',\n",
       "    'hidden_layer_activation = sigmoid(np.dot(inputs, weights_input_hidden) + biases_hidden)\\n',\n",
       "    'predictions = sigmoid(np.dot(hidden_layer_activation, weights_hidden_output) + biases_output)\\n',\n",
       "    '\\n',\n",
       "    'print(\"Final predictions:\")\\n',\n",
       "    'print(predictions)\\n',\n",
       "    '\\n'],\n",
       "   'id': 'f9edc28a6869fafe',\n",
       "   'outputs': [{'name': 'stdout',\n",
       "     'output_type': 'stream',\n",
       "     'text': ['Final predictions:\\n',\n",
       "      '[[0.05461413]\\n',\n",
       "      ' [0.94973818]\\n',\n",
       "      ' [0.94972971]\\n',\n",
       "      ' [0.05362768]]\\n']}],\n",
       "   'execution_count': 7},\n",
       "  {'metadata': {'ExecuteTime': {'end_time': '2025-05-08T07:17:00.583126Z',\n",
       "     'start_time': '2025-05-08T07:17:00.430443Z'}},\n",
       "   'cell_type': 'code',\n",
       "   'source': ['#动量\\n',\n",
       "    '# 动量参数\\n',\n",
       "    'momentum = 0.9\\n',\n",
       "    'velocity_w_input_hidden = np.zeros_like(weights_input_hidden)\\n',\n",
       "    'velocity_b_hidden = np.zeros_like(biases_hidden)\\n',\n",
       "    'velocity_w_hidden_output = np.zeros_like(weights_hidden_output)\\n',\n",
       "    'velocity_b_output = np.zeros_like(biases_output)\\n',\n",
       "    '\\n',\n",
       "    'for epoch in range(epochs):\\n',\n",
       "    '    # 前向传播、计算误差、反向传播的代码（与上面相同）\\n',\n",
       "    '\\n',\n",
       "    '    # 更新权重和偏置时加入动量\\n',\n",
       "    '    velocity_w_hidden_output = momentum * velocity_w_hidden_output + learning_rate * hidden_layer_activation.T.dot(d_output)\\n',\n",
       "    '    velocity_b_output = momentum * velocity_b_output + learning_rate * np.sum(d_output, axis=0)\\n',\n",
       "    '    velocity_w_input_hidden = momentum * velocity_w_input_hidden + learning_rate * inputs.T.dot(d_hidden_layer)\\n',\n",
       "    '    velocity_b_hidden = momentum * velocity_b_hidden + learning_rate * np.sum(d_hidden_layer, axis=0)\\n',\n",
       "    '\\n',\n",
       "    '    weights_hidden_output += velocity_w_hidden_output\\n',\n",
       "    '    biases_output += velocity_b_output\\n',\n",
       "    '    weights_input_hidden += velocity_w_input_hidden\\n',\n",
       "    '    biases_hidden += velocity_b_hidden\\n'],\n",
       "   'id': '77accfb38aa7a37c',\n",
       "   'outputs': [],\n",
       "   'execution_count': 8},\n",
       "  {'metadata': {'ExecuteTime': {'end_time': '2025-05-08T07:17:00.594164Z',\n",
       "     'start_time': '2025-05-08T07:17:00.588850Z'}},\n",
       "   'cell_type': 'code',\n",
       "   'source': ['#使用优化器\\n',\n",
       "    'import tensorflow as tf\\n',\n",
       "    '\\n',\n",
       "    '# 假设inputs是输入数据，weights是权重\\n',\n",
       "    'inputs = tf.constant([[0.0, 0.0], [0.0, 1.0], [1.0, 0.0], [1.0, 1.0]], dtype=tf.float32)\\n',\n",
       "    'weights = tf.random.normal([2, 3], dtype=tf.float32)  # 假设权重是2x3的矩阵\\n',\n",
       "    '\\n',\n",
       "    '# 确保inputs和weights的数据类型一致\\n',\n",
       "    'inputs = tf.cast(inputs, tf.float32)\\n',\n",
       "    'weights = tf.cast(weights, tf.float32)\\n',\n",
       "    '\\n',\n",
       "    '# 执行矩阵乘法\\n',\n",
       "    'hiddenlayeractivation = tf.matmul(inputs, weights)\\n',\n",
       "    '\\n',\n",
       "    'print(hiddenlayeractivation)\\n',\n",
       "    '\\n'],\n",
       "   'id': 'aa0b0930cccd3bb3',\n",
       "   'outputs': [{'name': 'stdout',\n",
       "     'output_type': 'stream',\n",
       "     'text': ['tf.Tensor(\\n',\n",
       "      '[[ 0.         0.         0.       ]\\n',\n",
       "      ' [ 1.7745123  1.0953304 -1.9704698]\\n',\n",
       "      ' [-3.2716508 -1.5692956 -1.4103999]\\n',\n",
       "      ' [-1.4971385 -0.4739653 -3.3808699]], shape=(4, 3), dtype=float32)\\n']}],\n",
       "   'execution_count': 9}],\n",
       " 'metadata': {'kernelspec': {'display_name': 'Python 3',\n",
       "   'language': 'python',\n",
       "   'name': 'python3'},\n",
       "  'language_info': {'codemirror_mode': {'name': 'ipython', 'version': 2},\n",
       "   'file_extension': '.py',\n",
       "   'mimetype': 'text/x-python',\n",
       "   'name': 'python',\n",
       "   'nbconvert_exporter': 'python',\n",
       "   'pygments_lexer': 'ipython2',\n",
       "   'version': '2.7.6'}},\n",
       " 'nbformat': 4,\n",
       " 'nbformat_minor': 5}"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 1
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
